package com.dms.modules.merchant.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.dms.modules.merchant.entity.Merchant;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

/**
 * 商家Mapper接口
 */
@Mapper
public interface MerchantMapper extends BaseMapper<Merchant> {
    
    /**
     * 通过用户名查询商家信息
     * 关联查询user表获取用户名、密码等信息
     * 
     * @param username 用户名
     * @return 商家信息
     */
    Merchant selectByUsername(@Param("username") String username);
    
    /**
     * 获取商家月销量
     */
    @Select("SELECT COALESCE(SUM(oi.quantity), 0) " +
            "FROM merchant m " +
            "LEFT JOIN order_item oi ON m.id = oi.merchant_id " +
            "LEFT JOIN `order` o ON oi.order_id = o.id " +
            "WHERE m.id = #{merchantId} " +
            "AND o.status = 3 " +
            "AND o.create_time >= DATE_SUB(CURDATE(), INTERVAL 1 MONTH)")
    int getMonthlySales(@Param("merchantId") Long merchantId);
    
    /**
     * 获取商家总销量
     */
    @Select("SELECT COALESCE(SUM(oi.quantity), 0) " +
            "FROM merchant m " +
            "LEFT JOIN order_item oi ON m.id = oi.merchant_id " +
            "LEFT JOIN `order` o ON oi.order_id = o.id " +
            "WHERE m.id = #{merchantId} " +
            "AND o.status = 3")
    int getTotalSales(@Param("merchantId") Long merchantId);
    
    /**
     * 获取商家商品数量
     */
    @Select("SELECT COUNT(p.id) " +
            "FROM merchant m " +
            "LEFT JOIN product p ON m.id = p.merchant_id " +
            "WHERE m.id = #{merchantId} " +
            "AND p.status = 1")
    int getProductCount(@Param("merchantId") Long merchantId);
} 